const Utils = require("../../utils/index.js")

function my_install(router, connection) {

    

   

    // 提交预约信息
    router.post('/post_appointment_info', async (ctx, next) => {
        ctx.body = await new Promise((resolve, reject) => {
            let name = ctx.request.body.name
            let tel = ctx.request.body.tel
            let address1 = ctx.request.body.address1
            let address2 = ctx.request.body.address2
            let time1 = ctx.request.body.time1
            let time2 = ctx.request.body.time2
            let time3 = ctx.request.body.time3
            let num = ctx.request.body.num
            let id = new Date().getTime()
            let goodsId = ctx.request.body.goodsId
            let sql_str = `insert into tappointment 
                (id , name , tel , address1 , address2 , time1 , time2 , time3 , num , goods_id) 
                values 
                (${id}, '${name}' , '${tel}' , '${address1}' , '${address2}' , '${time1}' , '${time2}' , '${time3}' , '${num}', '${goodsId}')`


            connection.query(sql_str, function (error, results, fields) {
                if (error) {
                    throw error
                } else {
                    resolve({
                        value: {
                            code: 200,
                            msg: '添加成功',
                            data: {
                                id,
                                name,
                                address1,
                                address2,
                                time1,
                                time2,
                                time3,
                                num
                            }
                        }
                    })
                }
            })
        })
    })


    // 根据手机号 获取预约信息
    router.post('/get_appointment_info_by_tel', async (ctx, next) => {
        ctx.body = await new Promise((resolve, reject) => {
            let tel = ctx.request.body.tel
            let sql_str = `SELECT a.* , b.title from tappointment as a LEFT JOIN tgoods as b on a.goods_id = b.id where a.tel ='${tel}';`


            connection.query(sql_str, function (error, results, fields) {
                if (error) {
                    throw error
                } else {
                    resolve({
                        value: {
                            code: 200,
                            msg: '查询成功',
                            data: results
                        }
                    })
                }
            })
        })
    })
}
module.exports = { my_install }